﻿using System;
using JetBrains.DataFlow;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Resolve;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.Psi.Xaml.Tree;

namespace GammaJul.ReSharper.GammaXaml.Psi.Xaml {

	[ReferenceProviderFactory]
	public class CaliburnXamlReferenceProviderFactory : IReferenceProviderFactory {
		private readonly CaliburnXamlReferenceProvider _referenceProvider = new CaliburnXamlReferenceProvider();

		public IReferenceFactory CreateFactory(IPsiSourceFile sourceFile, IFile file) {
			return file is IXamlFile && sourceFile.GetProject().IsCaliburnMicroProject() ? _referenceProvider : null;
		}
		
		public event Action OnChanged;

		private void RaiseOnChanged() {
			Action handler = OnChanged;
			if (handler != null)
				handler();
		}


		public CaliburnXamlReferenceProviderFactory(Lifetime lifetime, CaliburnCache caliburnCache) {
			caliburnCache.Invalidated.Advise(lifetime, RaiseOnChanged);
		}

	}

}